package com.cheetah.start.common.config;

import com.cheetah.start.common.config.property.UpLoadProprety;
import com.cheetah.start.common.constant.CommonConst;
import com.cheetah.start.common.interceptor.LoginInterceptor;
import com.cheetah.start.common.plugin.repeat.impl.RepeatSubmitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *
 * @author xjhmwj
 * @date 2021/6/11 18:11
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer {

    @Autowired
    private UpLoadProprety upLoadProprety;
    /** 重复提交拦截器 */
    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
    /** 登录拦截器 */
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径映射 */
        registry.addResourceHandler(CommonConst.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + upLoadProprety.getPath() + "/");
    }


    /**
     * 拦截器注册
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**");
    }
}
